/*
 * $Id: FileCopyTask.java,v 1.10 2010/09/21 18:07:10 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.processor.dispatcher.tasks;

import java.io.File;
import java.io.IOException;

import com.ge.healthcare.autosc.common.ASCLogger;
import com.ge.healthcare.autosc.common.util.ASCFileHandler;

/**
 * @author 212042764
 * 
 */
public class FileCopyTask extends BaseFileTransferTask {

	/**
	 * There is no login information for copy task
	 */
	public FileCopyTask() {
		this.ignoreFtConnInfo = true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ge.healthcare.autosc.processor.dispatcher.tasks.BaseFileTransferTask
	 * #putFile(java.io.File, java.lang.String, java.lang.String,
	 * java.lang.Long)
	 */
	protected void putFile(File srcFile, String destFileLoc,
			String incompleteFile, Long ftId) {
		String methodName = "putFile";
		File destFile = new File(destFileLoc);
		File incompleteDestFile = new File(incompleteFile);
		if (ASCFileHandler.copy(srcFile, incompleteDestFile)) {
			// If the file already exists we will overwrite. This only means
			// that you can defined a poor template
			if (destFile.exists()) {
				destFile.delete();
			}
			if (ASCFileHandler.rename(incompleteDestFile, destFile)) {
				this.markTaskFtStatusForSuccess(ftId);
				this.markSuccess(ftId, destFile);
				ASCLogger.debug(this.getClass(), methodName, new StringBuffer(
						"Successfully transferred src file:").append(
						srcFile.getAbsolutePath()).append(" as dest file:")
						.append(destFile.getAbsolutePath()).toString(), true);
			} else {
				this.markFailed(ftId, destFile.getAbsolutePath(),
						"Failed to rename incomplete file to:"
								+ destFile.getAbsolutePath(), null);
			}
		} else {
			this.markFailed(ftId, destFile.getAbsolutePath(),
					"Failed to rename incomplete file as:"
							+ incompleteDestFile.getAbsolutePath(), null);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ge.healthcare.autosc.processor.dispatcher.tasks.BaseFileTransferTask
	 * #getInCompleteFile(java.lang.String, java.lang.Long)
	 */
	protected String getInCompleteFile(String destFileLoc, Long ftId) {
		File incompleteDestFile = null;
		File destFile = new File(destFileLoc);
		try {
			incompleteDestFile = File.createTempFile(destFile.getName(),
					INCOMPLETE_EXT, destFile.getParentFile());
			return incompleteDestFile.getAbsolutePath();
		} catch (IOException ioException) {
			this.markFailed(ftId, destFile.getAbsolutePath(),
					"Failed to created incomplete destination file using:"
							+ destFile.getAbsolutePath(), ioException);
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ge.healthcare.autosc.processor.dispatcher.tasks.BaseFileTransferTask
	 * #validateTransferrable(java.io.File, java.io.File, java.lang.Long)
	 */
	protected boolean validateTransferrable(File srcFile, File destFile,
			Long ftId) {
		if (!srcFile.exists()) {
			this.markFailed(ftId, destFile.getAbsolutePath(),
					"Src file not available for transfer:"
							+ srcFile.getAbsolutePath(), null);
			return false;
		}
		if (!destFile.getParentFile().exists()) {
			if (!destFile.getParentFile().mkdirs()) {
				this.markFailed(ftId, destFile.getAbsolutePath(),
						"Failed to create parent directories for:"
								+ destFile.getAbsolutePath(), null);
				return false;
			}
		}
		if (!destFile.getParentFile().canWrite()) {
			this.markFailed(ftId, destFile.getAbsolutePath(),
					"Destination dir is not writable:"
							+ destFile.getAbsolutePath(), null);
			return false;
		}
		return true;
	}
}
